//用户地址
'use strict';
import React,{Component} from 'react';
import Header from '../common/header';//头部组件
import styles from './style/styles';//样式
import Icon from 'react-native-vector-icons';
import  MyStroage from '../common/myStroage';//异步存储

import * as URLConstant from '../constant/URLconstant';//接口类
import Util from '../common/util';//工具类
import Toast from '../common/ToastUtil';
import {
    View,
    Text,
    Image,
    TouchableOpacity,
    ScrollView,
    Alert
} from 'react-native';

export default class addressAdmin extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.successCallback = this.successCallback.bind(this);
        this.errorcallback = this.errorcallback.bind(this);
        this.getAllUserAdder = this.getAllUserAdder.bind(this);
        this.state = {
            addressData: [],
            user_id: '',
            onli: '',
        };
    }

    addAddress(title, type, rowData) {
        let that = this;
        this.props.navigation.push('AddAddress',{
            title:title,
            id:type,
            rowData:rowData,
            getOrderOnServer: that.getAllUserAdder
        })

    }


    async componentDidMount() {
        try {
            let that = this;
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo');
            if (userInfoDate == null) {
                return;
            }
            let user_id = userInfoDate.data.user_id;
            if (user_id != null || user_id != '') {
                that.setState({user_id: user_id});
                this.getAllUserAdder();
            }
        } catch (err) {
            LOG('address====11' + err);
        }
    }

    // 添加/修改回撤刷新页面的方法
    getAllUserAdder() {
        this.getAllUserAdder();
    }

    getAllUserAdder() {
        let that = this;
        let user_id = that.state.user_id;
        let onli = that.state.user_id;
        if (user_id != '' && user_id != null) {
            let address_list_url = URLConstant.ADDRESSADMIN_URL;//地址列表的url
            let data = [];
            data['user_id'] = user_id;
            Util.get(address_list_url, data, that.successCallback, that.errorcallback);
        }
    }

    //获取列表的数据成功
    successCallback(datas) {
        let that = this;
        //LOG(datas);
        if (datas['status'] == 0) {
            that.setState({addressData: datas['data']});
        }else{
            this.setState({
                addressData:[]
            })
        }

    }

    //请求失败调用的方法
    errorcallback(err) {
        LOG(err);
    }

    //删除对应的购物地址
    deleteAdderById(address_id) {
        // LOG(address_id);
        let alertMessage = '你确定要删除吗?'
        Alert.alert(
            '提示',
            alertMessage,
            [
                {text: '取消', onPress: () => this.cancelShow()},
                {text: '确定', onPress: () => this.DetermineShow(address_id)},
            ]
        )

    }

    DetermineShow(addr_id) {
        let that = this;
        let url = URLConstant.DELETE_ADDRESS_URL;
        let user_id = that.state.user_id;
        let data = [];
        data['user_id'] = user_id;
        data['addr_id'] = addr_id;
        Util.post(url, data, function (datas) {
            //LOG(JSON.stringify(datas));
            if (datas['status'] == 0) {
                Toast.show(datas['msg']);
                that.getAllUserAdder();
            }
        }, function (err) {
            Toast.show('删除失败' + err);
        });
    }

    cancelShow() {
        Toast.show("取消删除..", 3);
    }

    //设置默认收货地址的ID
    setDefaultAdderId(addr_id) {
        let that = this;
        let URL = URLConstant.DEFAULT_ADDER_URL;
        let user_id = that.state.user_id;
        let data = [];
        data['user_id'] = user_id;
        data['addr_id'] =addr_id ;
        Util.post(URL, data, function (datas) {
           // LOG(JSON.stringify(datas));
            if (datas['status'] == 0) {
                Toast.show('设置默认地址成功!');
                that.getAllUserAdder();
                //LOG('设置默认地址成功====111');
                if (that.props.type == 1) {
                    that.props.refresh();
                    that.props.navigator.pop();

                }
            } else {
                Toast.show('设置默认地址失败!');
            }
        }, function (err) {
            LOG(err);
        });

    }


    userAddress() {
        let that = this;
        let addressData = that.state.addressData;
        //LOG('ADDRESSDATA长度======' + addressData);
        let rows;
        let list = [];
        for (var i = 0; i < addressData.length; i++) {
            let rowData=addressData[i];
            let addr_id = addressData[i].addr_id;
            let icon;
            icon = (require('../imgs/address/unchoose.png'));
            if (addressData[i].is_default =='Y' ) {
                icon = (require('../imgs/address/choose.png'));
            }
            rows = (
                <View style={styles.address_wrap} key={i}>
                    <View style={styles.addressrow}>
                        <View style={styles.addressrow1}>
                            <Image
                                resizeMode="contain"
                                style={styles.address_img}
                                source={require('../imgs/address/myorder_man.png') }/>
                            <Text numberOfLines={1} style={styles.adr_txt}>{addressData[i].consignee}</Text>
                        </View>
                        <View style={styles.addressrow2}>
                            <Image
                                resizeMode="contain"
                                style={styles.address_img}
                                source={require('../imgs/address/myorder_phone.png') }/>
                            <Text numberOfLines={1} style={styles.adr_txt}>{addressData[i].phone_mob}</Text>
                        </View>
                    </View>
                    <Text style={{fontSize: 14, color: "gray",marginLeft: 40,}}>{addressData[i].region_name}</Text>
                    <Text style={{fontSize: 14, color: "gray",marginLeft: 40,}}>{addressData[i].address}</Text>

                    <View style={styles.addressrow3}>
                        <TouchableOpacity style={styles.addressrow4}
                                          onPress={() =>that.setDefaultAdderId(addr_id) }>
                            <Image
                                resizeMode="contain"
                                style={styles.address_img3}
                                source={icon}/>
                            <Text style={styles.adr_txt3}>点击设为收货地址</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={styles.addressrow5}
                                          onPress={()=>this.addAddress('修改收货地址',2,rowData)}>
                            <Image
                                resizeMode="contain"
                                style={styles.address_img3}
                                source={require('../imgs/address/edit.png') }/>
                            <Text style={styles.adr_txt3}>修改收货地址</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={styles.addressrow5}
                                          onPress={() => that.deleteAdderById(addr_id) }>
                            <Image
                                resizeMode="contain"
                                style={styles.address_img4}
                                source={require('../imgs/address/shop_delete.png') }/>
                            <Text style={styles.adr_txt3}>删除</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            )
            list.push(rows)
        }
        return list;
    }

    render() {
        return (
            <View style={{flex:1}}>
                <Header navigation={this.props.navigation} title={'地址管理'}/>
                <View style={styles.add_address}>
                    <Image
                        resizeMode="contain"
                        style={styles.add_img}
                        source={require('../imgs/address/address_01.png') }/>
                    <Text style={styles.new_adr} onPress={()=>this.addAddress('新增收货地址',1) }>+新增收货地址</Text>
                    <Image
                        resizeMode="contain"
                        style={styles.add_img}
                        source={require('../imgs/address/address_01.png') }/>
                </View>
                <ScrollView style={styles.bg_scrollview}>
                    {this.userAddress()}
                </ScrollView>
            </View>
        )
    }
}

